﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogKickStart.Models;
using MarkdownSharp;

namespace BlogKickStart.Controllers
{
    public class BlogPostController : Controller
    {
		private int pageSize = 5;
        //
        // GET: /BlogPost/
		//public ActionResult Index(int? BlogID, int? page)
		//{
		//    using (var data = new BlogKickstartContext())
		//    {
		//        var posts = data.Posts.Where(x => x.BlogID == BlogID);
		//        var results = posts.OrderBy(Post => Post.Date).Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
		//        ViewBag.page = page ?? 0;

		//        ViewBag.pages = Math.Floor((decimal)(data.Posts.Count() / pageSize));
		//        return View(results);
		//    }
		//    //return View();
		//}

		public ActionResult Index(int? blogID, int? page)
		{
			if (!blogID.HasValue)
			{
				blogID = 1;
			}
			using (var data = new BlogKickstartContext())
			{
				ViewBag.theme = data.Blogs.Single(n => n.BlogID == blogID.Value).Style.StyleNM;
				var posts = data.Posts.Where(n => n.BlogID == blogID.Value);
				var pagedPosts = posts.OrderBy(Post => Post.Date).Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
				ViewBag.page = page ?? 0;
				ViewBag.variable = 1;
				
				//ViewBag.pages = Math.Floor((decimal)(data.Posts.Count() / pageSize));
				var numberOfPages = Math.Floor((decimal)(data.Posts.Where(n => n.BlogID == blogID).Count() / pageSize));
				ViewBag.pages = numberOfPages;
				return View(pagedPosts);
			}
			//return View();
		}


		[HttpPost]
		public ActionResult Index(Post blogpost)
		{
			if (ModelState.IsValid)
			{
				using (var data = new BlogKickstartContext())
				{
					data.Posts.Add(blogpost);
					data.SaveChanges();
				}
				return RedirectToAction("Index");
			}

			return View(blogpost);
		}

		
		public ActionResult EditPost(int? postID, string unMarked, string Title, bool? delete)
		{
			using (var data = new BlogKickstartContext())
			{
				if (!postID.HasValue)
				{
					var newpost = data.Posts.Create();
					newpost.UnmarkedContent = " ";
					newpost.Title = " ";
					newpost.MarkedContent = " ";
					newpost.BlogID = 1;
					newpost.Date = System.DateTime.Now;
					data.Posts.Add(newpost);
					data.SaveChanges();
					return View(newpost);

				}	
				
				if (string.IsNullOrEmpty(unMarked) && string.IsNullOrEmpty(Title) && !delete.HasValue)
				{
					var post = data.Posts.Single(i => i.PostID == postID);
					return View(post	);
					

				}
				else if(delete.HasValue)
				{
					if (delete.Value)
					{
						data.Posts.Remove(data.Posts.Single(i => i.PostID == postID));
						data.SaveChanges();
						return View("Index");
					}
					return View();
				}
				else
				{
					var post = data.Posts.Single(i => i.PostID == postID);
					var markupdown = new Markdown();
					markupdown.Transform(unMarked);
					post.MarkedContent = markupdown.Transform(unMarked);
					post.UnmarkedContent = unMarked;
					post.Title = Title;
					data.SaveChanges();

					return RedirectToAction("Index");
				}
			}
		}

    }
}
